
#include "PreHeader.h"
#include "cBackGroundScroller.h"

cBackGroundScroller::cBackGroundScroller()
{
	m_batchNode = NULL;
	m_sprite1 = NULL;
	m_sprite2 = NULL;

	m_xPosition1 = 0.0f;
	m_xPosition2 = 0.0f;
}

cBackGroundScroller::~cBackGroundScroller()
{

}

void cBackGroundScroller::Init(const sBackGroundScrollerDesc& desc)
{
	m_desc = desc;

	m_batchNode = CCSpriteBatchNode::batchNodeWithFile(desc.spriteName.c_str(), 1);
	m_desc.parent->addChild(m_batchNode);

	m_batchNode->setAnchorPoint(ccp(0.0f, 0.0f));

	m_textureSize = m_batchNode->getTexture()->getContentSize();
	m_sprite1 = CCSprite::spriteWithTexture(m_batchNode->getTexture());
	m_sprite2 = CCSprite::spriteWithTexture(m_batchNode->getTexture());

	m_sprite1->setAnchorPoint(ccp(0.0f, 0.0f));
	m_sprite2->setAnchorPoint(ccp(0.0f, 0.0f));
	m_sprite2->setPosition(ccp(m_textureSize.width, 0.0f));

	m_xPosition1 = 0.0f;
	m_xPosition2 = m_textureSize.width;

	m_batchNode->addChild(m_sprite1);
	m_batchNode->addChild(m_sprite2);
}

void cBackGroundScroller::Update(tFloat dt)
{
	m_xPosition1 -= m_desc.speed * dt;

	if (m_xPosition1 <= -m_textureSize.width)
	{
		m_xPosition1 += m_textureSize.width * 2.0f;
	}
	m_sprite1->setPosition(ccp(m_xPosition1, 0.0f));

	m_xPosition2 -= m_desc.speed * dt;
	if (m_xPosition2 <= -m_textureSize.width)
	{
		m_xPosition2 += m_textureSize.width * 2.0f;
	}
	m_sprite2->setPosition(ccp(m_xPosition2, 0.0f));
}